package com.yoomiito.app.present.my.activity;

import com.yoomiito.app.Constant;
import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.model.my.MyCollectGoodsBean;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.my.MyCollectGoodsActivity;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class MyCollectGoodsPresenter extends BaseActPresenter<MyCollectGoodsActivity> {
    public MyCollectGoodsPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getCollectGoodsData(int page){
        if (NetworkUtils.checkNetPermission(getV())){
            getV().finishRefresh();
            return;
        }
        getV().showLoading();
        YmApi.getYmService()
                .getCollectGoods(page,Constant.PAGE_SIZE)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyCollectGoodsBean>getApiTransformerData())
                .compose(XApi.<MyCollectGoodsBean>getScheduler())
                .subscribe(new ApiSubscriber<MyCollectGoodsBean>(handleException()) {
                    @Override
                    public void onSuccess(MyCollectGoodsBean data) {
                        getV().setCollectGoodsData(data);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().showLoading();
                    }
                });
    }

    public void deletetCollectGood(String numId){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        getV().showLoading();
        YmApi.getYmService()
                .deleteCollectGood(numId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<Object>getApiTransformerData())
                .compose(XApi.<Object>getScheduler())
                .subscribe(new ApiSubscriber<Object>(handleException()) {
                    @Override
                    public void onSuccess(Object data) {
                        YMToast.showText("删除成功");
                        getV().deleteOk();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
//                        YMToast.showText("error");
                    }
                });
    }

}
